/**
 * Created by wzh on 2016/7/5.
 */
'use strict'
swan_pc
    .controller('CompanyDetailController',function($scope,$rootScope,$state,$timeout,$filter,CompanyInfo,ContactInfo){
        $scope.contactInCompanyId=-1;
        $scope.setContactInCompanyId=function(id){
            $scope.contactInCompanyId=id;
        }
        $scope.oppInCompanyId=-1;
        $scope.setCompanyInCompanyId=function(id){
            $scope.oppInCompanyId=id;
        }
        $scope.tabActive=1;
        $scope.changeTabActive=function(tab){
            var oldtab=$scope.tabActive;
            var result=true;
            $scope.tabActive=tab;
            if(tab==1||tab==2){
                if($rootScope.toState.name!='companydetail') result=$rootScope.goSref('companydetail');
                else if($rootScope.companyDirty)
                    if(!confirm("有数据未提交，确定要放弃修改么")){ $scope.tabActive=oldtab;return;}
                $scope.initDirty();
                if(result)  $scope.updatePresentCompany($scope.presentCompanyInfo.id);
            }
            else if(tab==3) result=$rootScope.goSref('companycontact');
            else if(tab==4) result=$rootScope.goSref('companyopp');
            else if(tab==5) result=$rootScope.goSref('companytask');
            else if(tab==6) result=$rootScope.goSref('companycalendar');
            if(!result) $scope.tabActive=oldtab;
        }
    })
    .controller('CompanyContactController',function($scope,$rootScope,$state,$timeout,$filter,CompanyInfo,ContactInfo,ngDialog,$q) {
        $scope.containSubCompany=false;
        function find(company,userCompanyName,recursion){
            if (company.companyBaseInfo.name==userCompanyName) return true;
            else if(recursion==true){
                for (var i=0;i<company.subCompanys.length;i++) {
                    if(find(company.subCompanys[i], userCompanyName, recursion)) return true;
                }
            }
            return false;
        }
        $scope.loadAll=function () {
            ContactInfo.query(function(result){
                $scope.contactBaseInfos=[];
                $scope.contactAllBaseInfos=result;
                angular.forEach(result,function(data){
                    if(find($scope.presentCompanyInfo,data.userCompanyName,$scope.containSubCompany)) $scope.contactBaseInfos.push(data);
                })
            })
        }
        $scope.containSubCompanyChange=function () {
            $scope.containSubCompany=!$scope.containSubCompany;
            $scope.loadAll();
        }
        $scope.loadAll();
        //筛选
        $scope.contactFilterInfos={
            name:"",
            businessConnection:{
                kh:false,
                hzhb:false,
                jzds:false,
                gys:false,
                qt:false
            },
            importance:{
                bxy:false,
                xy:false,
                zy:false,
                hzy:false,
                jqzy:false
            }
        };
        $scope.contactInfoFilter=function(item){
            if (item.baseInfo.name.indexOf( $scope.contactFilterInfos.name)<0&&(item.baseInfo.mobileNumberList[0]==undefined||item.baseInfo.mobileNumberList[0].value.indexOf( $scope.contactFilterInfos.name)<0)) return false;
            if(!(!$scope.contactFilterInfos.businessConnection.kh&&!$scope.contactFilterInfos.businessConnection.hzhb&&!$scope.contactFilterInfos.businessConnection.jzds&&!$scope.contactFilterInfos.businessConnection.gys&&!$scope.contactFilterInfos.businessConnection.qt
                ||item.personalityInfo!=undefined&&(
                $scope.contactFilterInfos.businessConnection.kh&&item.personalityInfo.businessConnection=="客户"||
                $scope.contactFilterInfos.businessConnection.hzhb&&item.personalityInfo.businessConnection=="合作伙伴"||
                $scope.contactFilterInfos.businessConnection.jzds&&item.personalityInfo.businessConnection=="竞争对手"||
                $scope.contactFilterInfos.businessConnection.gys&&item.personalityInfo.businessConnection=="供应商"||
                $scope.contactFilterInfos.businessConnection.qt&&item.personalityInfo.businessConnection=="其他"))) return false;
            if(!(!$scope.contactFilterInfos.importance.bxy&&!$scope.contactFilterInfos.importance.xy&&!$scope.contactFilterInfos.importance.zy&&!$scope.contactFilterInfos.importance.hzy&&!$scope.contactFilterInfos.importance.jqzy
                ||item.personalityInfo!=undefined&&(
                $scope.contactFilterInfos.importance.bxy&&item.personalityInfo.importanceToMe=="不需要"||
                $scope.contactFilterInfos.importance.xy&&item.personalityInfo.importanceToMe=="需要"||
                $scope.contactFilterInfos.importance.zy&&item.personalityInfo.importanceToMe=="重要"||
                $scope.contactFilterInfos.importance.hzy&&item.personalityInfo.importanceToMe=="很重要"||
                $scope.contactFilterInfos.importance.jqzy&&item.personalityInfo.importanceToMe=="极其重要"))) return false;
            return true;
        }
        //将联系人加入公司
        $scope.getContactById=function(id){
            var cont;
            angular.forEach($scope.contactAllBaseInfos,function(data) {
                if (data.id==id) cont=data;
            });
            return cont;
        }
        $scope.addContactsToCompany=function(){
            $scope.resultArray={};
            ngDialog.open({
                templateUrl:"scripts/components/myself/company/dialog/contactDialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.closeContactDialog=function(){
                        if(!confirm("确定要将这些联系人加入到"+$scope.presentCompanyInfo.companyBaseInfo.name+"中么？")) {
                            ngDialog.closeAll();
                            return;
                        }
                        var df=$q.defer();
                        var pm=df.promise;
                        var num=0;
                        var keys=Object.keys($scope.resultArray);
                        console.log(keys);
                        for (var i=0;i<keys.length;i++){
                            var contact=$scope.getContactById(keys[i]);
                            contact.userCompanyName=$scope.presentCompanyInfo.companyBaseInfo.name;
                            ContactInfo.update({},contact,function(result){
                                num+=1;
                                if (num==keys.length) df.resolve(num);
                            },function(result){
                                df.reject("保存失败")
                            })
                        }
                        pm.then(function(num){
                            alert("成功导入"+num+"位联系人");
                            ngDialog.closeAll();
                            $scope.loadAll()
                        },function (reason) {
                            alert("导入失败");
                            ngDialog.closeAll();
                        })
                    }
                }]
            })
        }
    })
